/*
 * Date         Author       Note
 * 2014.11.24   舞者无罪              机头测试窗口
 * 2015.07.01   wzwz         增加辛克失败等字符串
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <minigui/common.h>
#include <minigui/minigui.h>
#include <minigui/gdi.h>
#include <minigui/window.h>
#include <minigui/control.h>
#include "resource.h"
#include "keypane.h"
#include "func.h"
#include "lang.h"
#include "mac.h"
#include "com.h"
#include "mhctrlstate.h"
#include "info.h"
#include "mh/mh_knit.h"
#include "lang.h"
#include "mhedit.h"
#include "carriage.h"
#include "keypane_mac.h"
#include "mhlabel.h"
#include "globe.h"

static pthread_t tid;
static int puase ;
static int timer;
static char state;
static char esc_enable;

#define MOTOR_SPEED    180
#define DOUBLE_CLICK_TIME    50

enum {
    SELECT_ALL_DOWN = 2,
    SELECT_ALL_UP = 3,
};
enum {
    CYCLE_NONE = 0,
    CYCLE_ITEM = 1,
    CYCLE_GROUP = 2,
    CYCLE_ALL = 3
};
/*
 *
 */
static void CreateMacInterface(HWND hWnd)
{
    //create select
    int i = 0, j = 0;
    int xzq_left[4] = {48, 48 + 284, 48 + 400, 48 + 684};
    int xzq_top[2] = {40, 40 + 360};
    int xzq_id[8] = {ID_BACK_1SELECT, ID_BACK_2SELECT, ID_BACK_3SELECT, ID_BACK_4SELECT,
        ID_FRONT_1SELECT, ID_FRONT_2SELECT, ID_FRONT_3SELECT, ID_FRONT_4SELECT,
    };
    char *sc_num[8] = {"1", "2", "3", "4", "5", "6", "7", "8"};
    for(i = 0; i < 8; ++i)//group
    {
        for(j = 0; j < 8; ++j)//index
        {
            MH_CTRL_STATE *pctrl = NEW_MH_CTRL_STATE();
            pctrl->type = MH_CTRL_TYPE_SEL;
            pctrl->state = GetSelectState(g_knit_data, i < 4 ? 1 : 0, i & 3, j);
            CreateWindow (MH_CTRL_STATE_NAME,
                sc_num[i < 4 ? j : 7 - j],
                WS_CHILD | WS_VISIBLE,
                xzq_id[i] + (i < 4 ? j : 7 - j),
                xzq_left[i & 3],
                xzq_top[i / 4] + j * 20,
                20,
                20,
                hWnd, (int)pctrl);
        }
    }
    //create yarn
    int yarn_left[2] = {120, 120 + 400};
    int yarn_id[2] = {ID_LEFT_1YARN, ID_RGHT_1YARN};
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            MH_CTRL_STATE *pctrl = NEW_MH_CTRL_STATE();
            pctrl->type = MH_CTRL_TYPE_2YARN;
            pctrl->state = TRUE;
            CreateWindow (MH_CTRL_STATE_NAME,
                sc_num[j],
                WS_CHILD | WS_VISIBLE,
                yarn_id[i] + j,
                yarn_left[i] + j * 20,
                290,
                20,
                20,
                hWnd, (int)pctrl);
        }
    }
    //create dumu
    int dumu_left[4] = {88, 88 + 184, 88 + 400, 88 + 584};
    int dumu_top[2] = {181, 380};
    int dumu_id[2] = {ID_BACK_1DUMU, ID_FRONT_1DUMU};
    int dumu_sign_id[2] = {ID_BACK_DUMU_1SIGN, ID_FRONT_DUMU_1SIGN};
    for(i = 0; i < 2; ++i)
    {
        for(j = 0; j < 4; ++j)
        {
            MH_CTRL_STATE *ctrl = NEW_MH_CTRL_STATE();
            ctrl->type = MH_CTRL_TYPE_SEL;
            ctrl->state = MH_CTRL_STATE_DOWN;
            //ctrl->mode |= MH_CTRL_CLICK_DISABLE;
            CreateWindow(MH_CTRL_STATE_NAME,
                "180",
                WS_CHILD | WS_VISIBLE,
                dumu_id[i] + j,
                dumu_left[j],
                dumu_top[i],
                40,
                39,
                hWnd, (int)ctrl);
            ctrl = NEW_MH_CTRL_STATE();
            ctrl->type = MH_CTRL_TYPE_SIGN;
            ctrl->mode |= MH_CTRL_CLICK_DISABLE;
            CreateWindow(MH_CTRL_STATE_NAME,
                "",
                WS_CHILD | WS_VISIBLE,
                dumu_sign_id[i] + j,
                dumu_left[j] + 13,
                dumu_top[i] + (i ? -16 : 40),
                18,
                15,
                hWnd, (int)ctrl);
        }
    }
    //create sink
    int sink_top[2] = {234, 325};
    int sink_id[2] = {ID_BACK_1SINK, ID_FRONT_1SINK};
    int sink_sign_id[2] = {ID_BACK_SINK_1SIGN, ID_FRONT_SINK_1SIGN};
    for(i = 0; i < 2; ++i)
    {
        MH_CTRL_STATE *ctrl = NEW_MH_CTRL_STATE();
        ctrl->type = MH_CTRL_TYPE_SEL;
        ctrl->state = MH_CTRL_STATE_DOWN;
   //     ctrl->mode |= MH_CTRL_CLICK_DISABLE;
        CreateWindow(MH_CTRL_STATE_NAME,
            "300",
            WS_CHILD | WS_VISIBLE,
            sink_id[i],
            379,
            sink_top[i],
            40,
            39,
            hWnd, (int)ctrl);
        ctrl = NEW_MH_CTRL_STATE();
        ctrl->type = MH_CTRL_TYPE_SIGN;
        ctrl->mode |= MH_CTRL_CLICK_DISABLE;
        CreateWindow(MH_CTRL_STATE_NAME,
            "",
            WS_CHILD | WS_VISIBLE,
            sink_sign_id[i],
            392,
            sink_top[i] + (i ? -15 : 40),
            18,
            15,
            hWnd, (int)ctrl);
    }
    //cam
    int cam_id[4] = {ID_BACK_1CAM, ID_BACK_2CAM, ID_FRONT_1CAM, ID_FRONT_2CAM};
    int cam_top[8] = {96, 96, 96, 108, 108, 120, 120, 120};
    int cam_left[8] = {106, 165, 244, 106, 254, 106, 165, 254};
    int cam_width[8] = {50, 70, 50, 40, 40, 40, 70, 40};
    int cam_back[8] = {5, 6, 7, 3, 4, 0, 1, 2};
    int cam_front[8] = {0, 1, 2, 3, 4, 5, 6, 7};
    int cam_item[2][8] = {
        { 0, 1, 2, 3, 5, 3, 4, 5 },
        { 3, 4, 5, 3, 5, 0, 1, 2 },
    };
    for(i = 0; i < 4; ++i)
    {
        for(j = 0; j < 8; ++j)
        {
            MH_CTRL_STATE *pctrl = NEW_MH_CTRL_STATE();
            if(j == 0 || j == 2)
            {
                pctrl->type = MH_CTRL_TYPE_CAM;
            }
            else
            {
                pctrl->type = MH_CTRL_TYPE_SEL;      //MH_CTRL_TYPE_SEL;
            }
            int f0b1 = i < 2 ? 1 : 0;
            int state = GetCamState(g_knit_data, f0b1, i & 1, cam_item[f0b1][j]);
            pctrl->state = (j == 3 || j == 4) ? !state : state;
            CreateWindow(MH_CTRL_STATE_NAME,
                "",
                WS_CHILD | WS_VISIBLE,
                cam_id[i] + (i < 2 ? cam_back[j] : cam_front[j]),
                cam_left[j] + (i & 1 ? 400 : 0),
                cam_top[j] + (i < 2 ? 0 : 373),
                cam_width[j],
                10,
                hWnd, (int)pctrl);
        }
    }

    //cycle button
    int cycle_right[4] = {390, 470, 550, 630};
    const char *sc = GetString(867);
    int cycle_id[4] = {ID_CYCLE_GROUP, ID_CYCLE_GROUP + 1, ID_CYCLE_GROUP + 2, ID_CYCLE_GROUP + 3};
    for(i = 0; i < 4; ++i)
    {
        sc = GetString(867 + i);
        MH_CTRL_STATE *ctrl = NEW_MH_CTRL_STATE();
        ctrl->type = MH_CTRL_TYPE_CAM;
        ctrl->state = MH_CTRL_STATE_NORM;
        CreateWindow(MH_CTRL_STATE_NAME,
            sc,
            WS_CHILD,
            cycle_id[i],
            cycle_right[i],
            565,
            75,
            25,
            hWnd, (int)ctrl);

    }

    CreateWindow(MH_LABEL_NAME,
		GetString(861),
		WS_CHILD | WS_VISIBLE ,//| BS_LEFT,
		ID_EDIT3,
		100,
        555,
        200,
        30,
		hWnd, MH_LABEL_BKGND_BLACK | MH_LABEL_TEXT_RED);

    CreateWindow(MH_EDIT_NAME,
		GetString(871),
		WS_CHILD| WS_VISIBLE,
		ID_EDIT4,
		710,
        565,
        75,
        25,
		hWnd, MH_EDIT_BKGND_GREY);
    CreateWindow (MH_STATIC_NAME,
        "",
        WS_CHILD | WS_VISIBLE,
        ID_ESC,
        760,
        10,
        CX_ASK_BMP,
        CY_ASK_BMP,
        hWnd, (int)&g_bmp[BMP_FALSE]);
}
/*
 *
 */
static void ShowWarnInfo(HWND hWnd, int error)
{
    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
    HWND wnd = GetDlgItem(hWnd, ID_EDIT3);
    if(data.pwnd_mac)
    {
        const char *sc = NULL;
        switch(error)
        {
        case MH_ERROR_YARN:
            sc = MW_YARN_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_CAMF1:
        case MH_ERROR_CAMF2:
        case MH_ERROR_CAMB1:
        case MH_ERROR_CAMB2:
            sc = MW_CAM_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_XZQ:
            sc = MW_SELECT_WARN;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_DUMUF1:
            sc = MW_FRONT_1DUMU_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_DUMUF2:
            sc = MW_FRONT_2DUMU_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_DUMUB1:
            sc = MW_BACK_1DUMU_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_DUMUB2:
            sc = MW_BACK_2DUMU_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_DUMUF3:
            sc = MW_FRONT_3DUMU_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_DUMUF4:
            MW_FRONT_4DUMU_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_DUMUB3:
            sc = MW_BACK_3DUMU_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_DUMUB4:
            sc = MW_BACK_4DUMU_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_SINKF:
            sc = MW_FRONT_SINK_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        case MH_ERROR_SINKB:
            sc = MW_BACK_SINK_ERROR;
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
        break;
        case MH_ERROR_CAN:
            sc = GetString(484);
            SetWindowCaption(wnd, sc);
            InvalidateRect(wnd, NULL, TRUE);
            break;
        default:
            break;
        }
    }
}
/*
 *
 */
void UpdateCamItemState(HWND hWnd, int f0b1, int group, int item)
{
    enum {
        LEFT_2DUMU = 0,
        TRANSFER = 1,
        RIGHT_2DUMU = 2,
        LEFT_RECV = 3,
        TUCK = 4,
        RIGHT_RECV = 5
    };
    ushort id[2][2] = {
        {ID_FRONT_1CAM, ID_FRONT_2CAM},
        {ID_BACK_1CAM, ID_BACK_2CAM}
    };
    switch(item)
    {
    case 0:
    case 1:
    case 2:
        {
            HWND hTmp = GetDlgItem(hWnd, id[f0b1][group] + item);
            if(hTmp > 0)
            {
                SetCtrlState(hTmp, GetCamState(g_knit_data, f0b1, group, item));
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }
        break;
    case 3:
        {
            int state = GetCamState(g_knit_data, f0b1, group, item);
            HWND hTmp = GetDlgItem(hWnd, id[f0b1][group] + 3);
            if(hTmp > 0)
            {
                SetCtrlState(hTmp, !state);
                InvalidateRect(hTmp, NULL, TRUE);
            }
            hTmp = GetDlgItem(hWnd, id[f0b1][group] + 5);
            if(hTmp > 0)
            {
                SetCtrlState(hTmp, state);
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }
        break;
    case 4:
        {
            int state = GetCamState(g_knit_data, f0b1, group, item);
            HWND hTmp = GetDlgItem(hWnd, id[f0b1][group] + 6);
            if(hTmp > 0)
            {
                SetCtrlState(hTmp, state);
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }
        break;
    case 5:
        {
            int state = GetCamState(g_knit_data, f0b1, group, item);
            HWND hTmp = GetDlgItem(hWnd, id[f0b1][group] + 4);
            if(hTmp > 0)
            {
                SetCtrlState(hTmp, !state);
                InvalidateRect(hTmp, NULL, TRUE);
            }
            hTmp = GetDlgItem(hWnd, id[f0b1][group] + 7);
            if(hTmp > 0)
            {
                SetCtrlState(hTmp, state);
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }
        break;
    default:
        break;
    }
}


/*
 *
 */
static void OnBtnCmd(HWND hWnd, int nID)
{

    WND_EXTRA_DATA data;
    data.value = GetWindowAdditionalData(hWnd);
	switch(nID)
	{
    case ID_ESC:
        {
                int ret = WriteMacFile(MAC_PARAM_FILE, g_knit_data);
                if(ret)
                {
                    printf("write param: %d\n", ret);
                    AfxInfoBox(hWnd, NULL, GetString(481));
                }
                else
                {
                    MH_FILE_SYNC();
                }
                puase = FALSE;
                pthread_join(tid, NULL);
                MacRestore(hWnd);
        }
        break;
	case (ID_LEFT_1YARN)...(ID_LEFT_18YARN):
	    {
	        int index = nID - ID_LEFT_1YARN;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_yc_exec(GetCarrigeData(g_knit_data), 0, index);
            }
	    }
	    break;
	case (ID_RGHT_1YARN)...(ID_RGHT_18YARN):
	    {
            int index = nID - ID_RGHT_1YARN;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_yc_exec(GetCarrigeData(g_knit_data), 1, index);
            }
	    }
	    break;
	case (ID_FRONT_1SELECT)...(ID_FRONT_1SELECT + 7):
	    if(data.pwnd_mac)
	    {
            int index = nID - ID_FRONT_1SELECT;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_sel_exec(GetCarrigeData(g_knit_data), 0, index);
            }
	    }
	    break;
	case (ID_FRONT_2SELECT)...(ID_FRONT_2SELECT + 7):
	    if(data.pwnd_mac)
	    {
            int index = nID - ID_FRONT_2SELECT;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_sel_exec(GetCarrigeData(g_knit_data), 1, index);
            }
	    }
	    break;
	case (ID_FRONT_3SELECT)...(ID_FRONT_3SELECT + 7):
	    if(data.pwnd_mac)
	    {
            int index = nID - ID_FRONT_3SELECT;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_sel_exec(GetCarrigeData(g_knit_data), 4, index);
            }
	    }
	    break;
	case (ID_FRONT_4SELECT)...(ID_FRONT_4SELECT + 7):
	    if(data.pwnd_mac)
	    {
            int index = nID - ID_FRONT_4SELECT;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_sel_exec(GetCarrigeData(g_knit_data), 5, index);
            }
	    }
	    break;
	case (ID_BACK_1SELECT)...(ID_BACK_1SELECT + 7):
        if(data.pwnd_mac)
	    {
            int index = nID - ID_BACK_1SELECT;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_sel_exec(GetCarrigeData(g_knit_data), 2, index);
            }
	    }
	    break;
	case (ID_BACK_2SELECT)...(ID_BACK_2SELECT + 7):
	   if(data.pwnd_mac)
	    {
            int index = nID - ID_BACK_2SELECT;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_sel_exec(GetCarrigeData(g_knit_data), 3, index);
            }
	    }
	    break;
	case (ID_BACK_3SELECT)...(ID_BACK_3SELECT + 7):
	   if(data.pwnd_mac)
	    {
            int index = nID - ID_BACK_3SELECT;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_sel_exec(GetCarrigeData(g_knit_data), 6, index);
            }
	    }
	    break;
	case (ID_BACK_4SELECT)...(ID_BACK_4SELECT + 7):
	   if(data.pwnd_mac)
	    {
            int index = nID - ID_BACK_4SELECT;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_sel_exec(GetCarrigeData(g_knit_data), 7, index);
            }
	    }
	    break;
	case (ID_FRONT_1CAM)...(ID_FRONT_1CAM + 8):
	    if(data.pwnd_mac)
	    {
            int index = nID - ID_FRONT_1CAM;
            int id[8] = {3, 4, 5, 0, 2, 0, 1,2};
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_cam_exec(GetCarrigeData(g_knit_data), 0, id[index]);
            }
	    }
	    break;
	case (ID_FRONT_2CAM)...(ID_FRONT_2CAM + 8):
	 if(data.pwnd_mac)
	    {
            int index = nID - ID_FRONT_2CAM;
            int id[8] = {3, 4, 5, 0, 2, 0, 1,2};
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_cam_exec(GetCarrigeData(g_knit_data), 2, id[index]);
            }
	    }
	    break;
	case (ID_BACK_1CAM)...(ID_BACK_1CAM + 8):
		 if(data.pwnd_mac)
	    {
            int index = nID - ID_BACK_1CAM;
            int id[8] = {3, 4, 5, 3, 5, 0, 1,2};
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_cam_exec(GetCarrigeData(g_knit_data), 1, id[index]);
            }
	    }
	    break;
	case (ID_BACK_2CAM)...(ID_BACK_2CAM + 8):
	   	 if(data.pwnd_mac)
	    {
            int index = nID - ID_BACK_2CAM;
            int id[8] = {3, 4, 5, 3, 5, 0, 1,2};
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                carriage_cam_exec(GetCarrigeData(g_knit_data), 3, id[index]);
            }
	    }
	    break;
    case (ID_FRONT_1DUMU)...(ID_FRONT_1DUMU + 3):
        if(data.pwnd_mac)
	    {
            int index = nID - ID_FRONT_1DUMU;
            if (GetCarrigeData(g_knit_data) !=NULL)
            {
                if(index < 2)
                {
                    carriage_motor_exec(GetCarrigeData(g_knit_data), 0, index);
                }
                else
                {
                    carriage_motor_exec(GetCarrigeData(g_knit_data), 2, index - 2);
                }

            }
	    }
	    break;
    case (ID_BACK_1DUMU)...(ID_BACK_1DUMU + 3):
       	 if(data.pwnd_mac)
	    {
            int index = nID - ID_BACK_1DUMU;
           if (GetCarrigeData(g_knit_data) !=NULL)
            {
                if(index < 2)
                {
                     carriage_motor_exec(GetCarrigeData(g_knit_data), 1, index);
                }
                else
                {
                     carriage_motor_exec(GetCarrigeData(g_knit_data), 3, index - 2);
                }
            }
	    }
	    break;
    case ID_FRONT_1SINK:
    case ID_BACK_1SINK:
        if(data.pwnd_mac)
	    {
            int index = nID - ID_FRONT_1SINK;
           if (GetCarrigeData(g_knit_data) !=NULL)
            {
                if(index == 0)
                {
                     carriage_motor_exec(GetCarrigeData(g_knit_data), 4, 1);
                }
                else if(index == 10)
                {
                     carriage_motor_exec(GetCarrigeData(g_knit_data), 4,0);
                }
            }
	    }
	    break;
    case ID_CYCLE_GROUP:
    case ID_CYCLE_GROUP + 1:
    case ID_CYCLE_GROUP + 2:
    case ID_CYCLE_GROUP + 3:
        {
            MacCycleButton(hWnd, nID);
        }
	    break;
    case ID_EDIT4:
        {
            int id = GetMacFocusId(g_knit_data);
            switch(id)
            {
                case (ID_FRONT_1DUMU)...(ID_FRONT_1DUMU + 3):
                case (ID_BACK_1DUMU)...(ID_BACK_1DUMU + 3):
                case ID_FRONT_1SINK:
                case ID_BACK_1SINK:
                {
                    HWND hTmp = GetDlgItem(hWnd, ID_EDIT4);
                    RECT rect;
                    GetClientRect(hTmp, &rect);
                    ClientToScreen(hTmp, &rect.left, &rect.top);

                    ClientToScreen(hTmp, &rect.right, &rect.bottom);
                    int value = atoi(GetWindowCaption(hTmp));
                    CreateKeyboardWindow(hWnd, rect, 0, 1000, value);
                }
                break;
            }
        }
        break;
	default:
		break;
	}
}
/*
 *
 */
static void OnKeyDown(HWND hWnd, uint key)
{
    if(key == SCANCODE_ESCAPE)
	{
		OnBtnCmd(hWnd, ID_ESC);
	}
    else if(key == SCANCODE_CURSORBLOCKLEFT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        switch(nID)
        {
            case (ID_FRONT_2SELECT)...(ID_FRONT_2SELECT + 7):
            {
                int index = nID - ID_FRONT_2SELECT;
                int id[8] = {ID_FRONT_1CAM + 7, ID_FRONT_1CAM + 7, ID_FRONT_1CAM + 7,
                ID_FRONT_1CAM + 4, ID_FRONT_1CAM + 2, ID_FRONT_1CAM + 2,  ID_FRONT_1CAM + 2, ID_FRONT_1DUMU + 1};
                nID = id[index];
            }
            break;
            case (ID_FRONT_3SELECT)...(ID_FRONT_3SELECT + 7):
            {
                int index = nID - ID_FRONT_3SELECT;
                nID = ID_FRONT_2SELECT + index;
            }
            break;
            case (ID_FRONT_4SELECT)...(ID_FRONT_4SELECT + 7):
            {
                int index = nID - ID_FRONT_4SELECT;
                int id[8] = {ID_FRONT_2CAM + 7, ID_FRONT_2CAM + 7, ID_FRONT_2CAM + 7,
                ID_FRONT_2CAM + 4, ID_FRONT_2CAM + 2,  ID_FRONT_2CAM + 2,  ID_FRONT_2CAM + 2, ID_FRONT_1DUMU + 3};
                nID = id[index];
            }
            break;
            case (ID_BACK_2SELECT)...(ID_BACK_2SELECT + 7):
            {
                int index = nID - ID_BACK_2SELECT;
                int id[8] = {ID_BACK_1CAM + 7, ID_BACK_1CAM + 7, ID_BACK_1CAM + 7,
                ID_BACK_1CAM + 4, ID_BACK_1CAM + 2, ID_BACK_1CAM + 2, ID_BACK_1CAM + 2, ID_BACK_1DUMU + 1};
                nID = id[index];
            }
            break;
            case (ID_BACK_3SELECT)...(ID_BACK_3SELECT + 7):
            {
                int index = nID - ID_BACK_3SELECT;
                nID = index + ID_BACK_2SELECT;
            }
            break;
            case (ID_BACK_4SELECT)...(ID_BACK_4SELECT + 7):
            {
                int index = nID - ID_BACK_4SELECT;
                int id[8] = {ID_BACK_2CAM + 7, ID_BACK_2CAM + 7, ID_BACK_2CAM + 7,
                ID_BACK_2CAM + 4, ID_BACK_2CAM + 2, ID_BACK_2CAM + 2, ID_BACK_2CAM + 2, ID_BACK_1DUMU + 3};
                nID = id[index];
            }
            break;
            case (ID_LEFT_1YARN)...(ID_LEFT_1YARN + 7):
            case (ID_RGHT_1YARN)...(ID_RGHT_1YARN + 7):
            {
                nID--;
                if(nID == ID_RGHT_1YARN)
                {
                    nID = ID_BACK_1SINK;
                }
            }
            break;
            case(ID_FRONT_1DUMU)...(ID_FRONT_1DUMU + 3):
            {
                if(nID == ID_FRONT_1DUMU + 2)
                {
                    nID = ID_FRONT_3SELECT + 7;
                }
                else if(nID == ID_FRONT_1DUMU )
                {
                    nID = ID_FRONT_1SELECT +7;
                }
                else
                {
                    nID--;
                }
            }
            break;
            case(ID_BACK_1DUMU)...(ID_BACK_1DUMU + 3):
            {
                if(nID == ID_BACK_1DUMU + 2)
                {
                    nID = ID_BACK_3SELECT + 7;
                }
                else if(nID == ID_BACK_1DUMU)
                {
                    nID = ID_BACK_1SELECT +7;
                }
                else
                {
                    nID--;
                }
            }
            break;
            case(ID_BACK_1CAM)...(ID_BACK_1CAM + 7):
            {
                int index = nID - ID_BACK_1CAM;
                int id[8] = {ID_BACK_1SELECT + 4, ID_BACK_1CAM , ID_BACK_1CAM + 1,
                ID_BACK_1SELECT + 3, ID_BACK_1CAM + 3, ID_BACK_1SELECT + 2, ID_BACK_1CAM + 5, ID_BACK_1CAM +6};
                nID = id[index];
            }
            break;
            case(ID_BACK_2CAM)...(ID_BACK_2CAM + 7):
            {
                int index = nID - ID_BACK_2CAM;
                int id[8] = {ID_BACK_3SELECT + 4, ID_BACK_2CAM , ID_BACK_2CAM + 1,
                ID_BACK_3SELECT + 3, ID_BACK_2CAM + 3, ID_BACK_3SELECT + 2, ID_BACK_2CAM + 5, ID_BACK_2CAM +6};
                nID = id[index];
            }
            break;
            case(ID_FRONT_1CAM)...(ID_FRONT_1CAM + 7):
            {
                int index = nID - ID_FRONT_1CAM;
                int id[8] = {ID_FRONT_1SELECT + 4, ID_FRONT_1CAM , ID_FRONT_1CAM + 1,
                ID_FRONT_1SELECT + 3, ID_FRONT_1CAM + 3, ID_FRONT_1SELECT + 2, ID_FRONT_1CAM + 5, ID_FRONT_1CAM +6};
                nID = id[index];
            }
            break;
            case(ID_FRONT_2CAM)...(ID_FRONT_2CAM + 7):
            {
                int index = nID - ID_FRONT_2CAM;
                int id[8] = {ID_FRONT_3SELECT + 4, ID_FRONT_2CAM , ID_FRONT_2CAM + 1,
                ID_FRONT_3SELECT + 3, ID_FRONT_2CAM + 3, ID_FRONT_3SELECT + 2, ID_FRONT_2CAM + 5, ID_FRONT_2CAM +6};
                nID = id[index];
            }
            break;
            case ID_BACK_1SINK:
            case ID_FRONT_1SINK:
            {
                if(nID == ID_BACK_1SINK)
                {
                    nID = ID_BACK_1DUMU + 1;
                }
                else
                {
                    nID = ID_FRONT_1DUMU + 1;
                }
            }
            break;
        }
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_CURSORBLOCKRIGHT)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        switch(nID)
        {
            case (ID_FRONT_1SELECT)...(ID_FRONT_1SELECT + 7):
            {
                int index = nID - ID_FRONT_1SELECT;
                int id[8] = {ID_FRONT_1CAM + 5, ID_FRONT_1CAM + 5, ID_FRONT_1CAM + 5,
                ID_FRONT_1CAM + 3, ID_FRONT_1CAM, ID_FRONT_1CAM, ID_FRONT_1CAM, ID_FRONT_1DUMU};
                nID = id[index];
            }
            break;
            case (ID_FRONT_2SELECT)...(ID_FRONT_2SELECT + 7):
            {
                int index = nID - ID_FRONT_2SELECT;
                nID = ID_FRONT_3SELECT + index;
            }
            break;
            case (ID_FRONT_3SELECT)...(ID_FRONT_3SELECT + 7):
            {
                int index = nID - ID_FRONT_3SELECT;
                int id[8] = {ID_FRONT_2CAM + 5, ID_FRONT_2CAM + 5, ID_FRONT_2CAM + 5,
                ID_FRONT_2CAM + 3, ID_FRONT_2CAM, ID_FRONT_2CAM, ID_FRONT_2CAM, ID_FRONT_1DUMU + 2};
                nID = id[index];
            }
            break;
            case (ID_BACK_1SELECT)...(ID_BACK_1SELECT + 7):
            {
                int index = nID - ID_BACK_1SELECT;
                int id[8] = {ID_BACK_1CAM + 5, ID_BACK_1CAM + 5, ID_BACK_1CAM + 5,
                ID_BACK_1CAM + 3, ID_BACK_1CAM, ID_BACK_1CAM, ID_BACK_1CAM, ID_BACK_1DUMU};
                nID = id[index];
            }
            break;
            case (ID_BACK_2SELECT)...(ID_BACK_2SELECT + 7):
            {
                int index = nID - ID_BACK_2SELECT;
                nID = index + ID_BACK_3SELECT;
            }
            break;
            case (ID_BACK_3SELECT)...(ID_BACK_3SELECT + 7):
            {
                int index = nID - ID_BACK_3SELECT;
                int id[8] = {ID_BACK_2CAM + 5, ID_BACK_2CAM + 5, ID_BACK_2CAM + 5,
                ID_BACK_2CAM + 3, ID_BACK_2CAM, ID_BACK_2CAM, ID_BACK_2CAM, ID_BACK_1DUMU + 2};
                nID = id[index];
            }
            break;
            case (ID_LEFT_1YARN)...(ID_LEFT_1YARN + 7):
            case (ID_RGHT_1YARN)...(ID_RGHT_1YARN + 7):
            {
                nID++;
                if(nID == ID_LEFT_1YARN + 8)
                {
                    nID = ID_FRONT_1SINK;
                }
            }
            break;
            case(ID_FRONT_1DUMU)...(ID_FRONT_1DUMU + 3):
            {
                if(nID == ID_FRONT_1DUMU + 1)
                {
                    nID = ID_FRONT_2SELECT + 7;
                }
                else if(nID == ID_FRONT_1DUMU +3)
                {
                    nID = ID_FRONT_4SELECT +7;
                }
                else
                {
                    nID++;
                }
            }
            break;
            case(ID_BACK_1DUMU)...(ID_BACK_1DUMU + 3):
            {
                if(nID == ID_BACK_1DUMU + 1)
                {
                    nID = ID_BACK_2SELECT + 7;
                }
                else if(nID == ID_BACK_1DUMU +3)
                {
                    nID = ID_BACK_4SELECT +7;
                }
                else
                {
                    nID++;
                }
            }
            break;
            case(ID_BACK_1CAM)...(ID_BACK_1CAM + 7):
            {
                int index = nID - ID_BACK_1CAM;
                int id[8] = {ID_BACK_1CAM + 1, ID_BACK_1CAM + 2, ID_BACK_2SELECT + 4,
                ID_BACK_1CAM + 4, ID_BACK_2SELECT + 3, ID_BACK_1CAM + 6, ID_BACK_1CAM + 7, ID_BACK_2SELECT + 2};
                nID = id[index];
            }
            break;
            case(ID_BACK_2CAM)...(ID_BACK_2CAM + 7):
            {
                int index = nID - ID_BACK_2CAM;
                int id[8] = {ID_BACK_2CAM + 1, ID_BACK_2CAM + 2, ID_BACK_4SELECT + 4,
                ID_BACK_2CAM + 4, ID_BACK_4SELECT + 3, ID_BACK_2CAM + 6, ID_BACK_2CAM + 7, ID_BACK_4SELECT + 2};
                nID = id[index];
            }
            break;
            case(ID_FRONT_1CAM)...(ID_FRONT_1CAM + 7):
            {
                int index = nID - ID_FRONT_1CAM;
                int id[8] = {ID_FRONT_1CAM + 1, ID_FRONT_1CAM + 2, ID_FRONT_2SELECT + 4,
                ID_FRONT_1CAM + 4, ID_FRONT_2SELECT + 3, ID_FRONT_1CAM + 6, ID_FRONT_1CAM + 7, ID_FRONT_2SELECT + 2};
                nID = id[index];
            }
            break;
            case(ID_FRONT_2CAM)...(ID_FRONT_2CAM + 7):
            {
                int index = nID - ID_FRONT_2CAM;
                int id[8] = {ID_FRONT_2CAM + 1, ID_FRONT_2CAM + 2, ID_FRONT_4SELECT + 4,
                ID_FRONT_2CAM + 4, ID_FRONT_4SELECT + 3, ID_FRONT_2CAM + 6, ID_FRONT_2CAM + 7, ID_FRONT_4SELECT + 2};
                nID = id[index];
            }
            break;
            case ID_BACK_1SINK:
            case ID_FRONT_1SINK:
            {
                if(nID == ID_BACK_1SINK)
                {
                    nID = ID_BACK_1DUMU + 2;
                }
                else
                {
                    nID = ID_FRONT_1DUMU + 2;
                }
            }
            break;
        }
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_CURSORBLOCKUP)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        switch(nID)
        {
            case (ID_FRONT_1SELECT)...(ID_FRONT_1SELECT + 7):
            case (ID_FRONT_2SELECT)...(ID_FRONT_2SELECT + 7):
            case (ID_FRONT_3SELECT)...(ID_FRONT_3SELECT + 7):
            case (ID_FRONT_4SELECT)...(ID_FRONT_4SELECT + 7):
            {
                nID++;
                if(nID == ID_FRONT_1SELECT + 8)
                {
                    nID = ID_BACK_1SELECT + 7;
                }
                else if(nID == ID_FRONT_2SELECT + 8)
                {
                    nID = ID_FRONT_1SINK;
                }
                else if(nID == ID_FRONT_3SELECT +8)
                {
                    nID = ID_FRONT_1SINK;
                }
                else if(nID == ID_FRONT_4SELECT + 8)
                {
                    nID = ID_BACK_4SELECT + 7;
                }
            }
            break;
            case (ID_BACK_1SELECT)...(ID_BACK_1SELECT + 7):
            case (ID_BACK_2SELECT)...(ID_BACK_2SELECT + 7):
            case (ID_BACK_3SELECT)...(ID_BACK_3SELECT + 7):
            case (ID_BACK_4SELECT)...(ID_BACK_4SELECT + 7):
            {
                nID--;
            }
            break;
            case (ID_LEFT_1YARN)...(ID_LEFT_1YARN + 7):
            {
                if(nID == ID_LEFT_1YARN || nID == ID_LEFT_1YARN + 1)
                {
                    nID = ID_BACK_1DUMU;
                }
                else if(nID == ID_LEFT_1YARN + 6 ||nID == ID_LEFT_1YARN + 7)
                {
                    nID = ID_BACK_1DUMU + 1;
                }
                else
                {
                    nID = ID_BACK_1CAM + 1;
                }
            }
            break;
             case (ID_RGHT_1YARN)...(ID_RGHT_1YARN + 7):
            {
                if(nID == ID_RGHT_1YARN || nID == ID_RGHT_1YARN + 1)
                {
                    nID = ID_BACK_1DUMU + 2;
                }
                else if(nID == ID_RGHT_1YARN + 6 ||nID == ID_RGHT_1YARN + 7)
                {
                    nID = ID_BACK_1DUMU + 3;
                }
                else
                {
                    nID = ID_BACK_2CAM + 1;
                }
            }
            break;
            case(ID_FRONT_1DUMU)...(ID_FRONT_1DUMU + 3):
            {
                if(nID == ID_FRONT_1DUMU)
                {
                    nID = ID_LEFT_1YARN;
                }
                else if(nID == ID_FRONT_1DUMU + 1)
                {
                    nID = ID_LEFT_1YARN + 7;
                }
                else if(nID == ID_FRONT_1DUMU + 2)
                {
                    nID = ID_RGHT_1YARN;
                }
                else if(nID == ID_FRONT_1DUMU +3)
                {
                    nID = ID_RGHT_1YARN +7;
                }
            }
            break;
            case(ID_BACK_1DUMU)...(ID_BACK_1DUMU + 3):
            {
                if(nID == ID_BACK_1DUMU)
                {
                    nID = ID_BACK_1CAM;
                }
                else if(nID == ID_BACK_1DUMU + 1)
                {
                    nID = ID_BACK_1CAM + 2;
                }
                else if(nID == ID_BACK_1DUMU + 2)
                {
                    nID = ID_BACK_2CAM;
                }
                else if(nID == ID_BACK_1DUMU + 3)
                {
                    nID = ID_BACK_2CAM + 2;
                }
            }
            break;
            case(ID_BACK_1CAM)...(ID_BACK_1CAM + 7):
            {
                int index = nID - ID_BACK_1CAM;
                int id[5] = {ID_BACK_1CAM + 3, ID_BACK_1CAM + 6, ID_BACK_1CAM + 4,
                ID_BACK_1CAM + 5, ID_BACK_1CAM +7};
                nID = id[index];
            }
            break;
            case(ID_BACK_2CAM)...(ID_BACK_2CAM + 7):
            {
                int index = nID - ID_BACK_2CAM;
                int id[5] = {ID_BACK_2CAM + 3, ID_BACK_2CAM + 6, ID_BACK_2CAM + 4,
                ID_BACK_2CAM + 5, ID_BACK_2CAM +7};
                nID = id[index];
            }
            break;
            case(ID_FRONT_1CAM)...(ID_FRONT_1CAM + 7):
            {
                int index = nID - ID_FRONT_1CAM;
                int id[8] = {ID_FRONT_1DUMU, ID_LEFT_1YARN + 3, ID_FRONT_1DUMU + 1,
                ID_FRONT_1CAM, ID_FRONT_1CAM + 2, ID_FRONT_1CAM +3, ID_FRONT_1CAM + 1, ID_FRONT_1CAM + 4};
                nID = id[index];
            }
            break;
            case(ID_FRONT_2CAM)...(ID_FRONT_2CAM + 7):
            {
                int index = nID - ID_FRONT_2CAM;
                int id[8] = {ID_FRONT_1DUMU + 2, ID_RGHT_1YARN + 3, ID_FRONT_1DUMU + 3,
                ID_FRONT_2CAM, ID_FRONT_2CAM + 2, ID_FRONT_2CAM +3, ID_FRONT_2CAM + 1, ID_FRONT_2CAM + 4};
                nID = id[index];
            }
            break;
            case ID_BACK_1SINK:
            case ID_FRONT_1SINK:
            {
                if(nID == ID_BACK_1SINK)
                {
                    nID = ID_BACK_3SELECT + 7;
                }
                else
                {
                    nID = ID_BACK_1SINK;
                }
            }
            break;
        }
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_CURSORBLOCKDOWN)
    {
        int nID = GetDlgCtrlID(GetFocusChild(hWnd));
        switch(nID)
        {
            case (ID_BACK_1SELECT)...(ID_BACK_1SELECT + 7):
            case (ID_BACK_2SELECT)...(ID_BACK_2SELECT + 7):
            case (ID_BACK_3SELECT)...(ID_BACK_3SELECT + 7):
            case (ID_BACK_4SELECT)...(ID_BACK_4SELECT + 7):
            {
                nID++;
                if(nID == ID_BACK_1SELECT + 8)
                {
                    nID = ID_FRONT_1SELECT + 7;
                }
                else if(nID == ID_BACK_2SELECT + 8)
                {
                    nID = ID_BACK_1SINK;
                }
                else if(nID == ID_BACK_3SELECT +8)
                {
                    nID = ID_BACK_1SINK;
                }
                else if(nID == ID_BACK_4SELECT + 8)
                {
                    nID = ID_FRONT_4SELECT + 7;
                }
            }
            break;
            case (ID_FRONT_1SELECT)...(ID_FRONT_1SELECT + 7):
            case (ID_FRONT_2SELECT)...(ID_FRONT_2SELECT + 7):
            case (ID_FRONT_3SELECT)...(ID_FRONT_3SELECT + 7):
            case (ID_FRONT_4SELECT)...(ID_FRONT_4SELECT + 7):
            {
                nID--;
            }
            break;
            case (ID_LEFT_1YARN)...(ID_LEFT_1YARN + 7):
            {
                if(nID == ID_LEFT_1YARN || nID == ID_LEFT_1YARN + 1)
                {
                    nID = ID_FRONT_1DUMU;
                }
                else if(nID == ID_LEFT_1YARN + 6 ||nID == ID_LEFT_1YARN + 7)
                {
                    nID = ID_FRONT_1DUMU + 1;
                }
                else
                {
                    nID = ID_FRONT_1CAM + 1;
                }
            }
            break;
             case (ID_RGHT_1YARN)...(ID_RGHT_1YARN + 7):
            {
                if(nID == ID_RGHT_1YARN || nID == ID_RGHT_1YARN + 1)
                {
                    nID = ID_FRONT_1DUMU + 2;
                }
                else if(nID == ID_RGHT_1YARN + 6 ||nID == ID_RGHT_1YARN + 7)
                {
                    nID = ID_FRONT_1DUMU + 3;
                }
                else
                {
                    nID = ID_FRONT_2CAM + 1;
                }
            }
            break;
            case(ID_FRONT_1DUMU)...(ID_FRONT_1DUMU + 3):
            {
                if(nID == ID_FRONT_1DUMU)
                {
                    nID = ID_FRONT_1CAM;
                }
                else if(nID == ID_FRONT_1DUMU + 1)
                {
                    nID = ID_FRONT_1CAM + 2;
                }
                else if(nID == ID_FRONT_1DUMU + 2)
                {
                    nID = ID_FRONT_2CAM;
                }
                else if(nID == ID_FRONT_1DUMU +3)
                {
                    nID = ID_FRONT_2CAM +2;
                }
            }
            break;
            case(ID_BACK_1DUMU)...(ID_BACK_1DUMU + 3):
            {
                if(nID == ID_BACK_1DUMU)
                {
                    nID = ID_LEFT_1YARN;
                }
                else if(nID == ID_BACK_1DUMU + 1)
                {
                    nID = ID_LEFT_1YARN + 7;
                }
                else if(nID == ID_BACK_1DUMU + 2)
                {
                    nID = ID_RGHT_1YARN;
                }
                else if(nID == ID_BACK_1DUMU + 3)
                {
                    nID = ID_RGHT_1YARN + 7;
                }
            }
            break;
            case(ID_BACK_1CAM)...(ID_BACK_1CAM + 7):
            {
                int index = nID - ID_BACK_1CAM;
                int id[8] = {ID_BACK_1DUMU, ID_LEFT_1YARN + 3, ID_BACK_1DUMU + 1,
                ID_BACK_1CAM, ID_BACK_1CAM + 2, ID_BACK_1CAM +3, ID_BACK_1CAM + 1, ID_BACK_1CAM + 4};
                nID = id[index];
            }
            break;
            case(ID_BACK_2CAM)...(ID_BACK_2CAM + 7):
            {
                int index = nID - ID_BACK_2CAM;
                int id[8] = {ID_BACK_1DUMU + 2, ID_RGHT_1YARN + 3, ID_BACK_1DUMU + 3,
                ID_BACK_2CAM, ID_BACK_2CAM + 2, ID_BACK_2CAM +3, ID_BACK_2CAM + 1, ID_BACK_2CAM + 4};
                nID = id[index];
            }
            break;
            case(ID_FRONT_1CAM)...(ID_FRONT_1CAM + 7):
            {
                int index = nID - ID_FRONT_1CAM;
                int id[5] = {ID_FRONT_1CAM + 3, ID_FRONT_1CAM + 6, ID_FRONT_1CAM + 4,
                ID_FRONT_1CAM + 5, ID_FRONT_1CAM +7};
                nID = id[index];
            }
            break;
            case(ID_FRONT_2CAM)...(ID_FRONT_2CAM + 7):
            {
                int index = nID - ID_FRONT_2CAM;
                int id[5] = {ID_FRONT_2CAM + 3, ID_FRONT_2CAM + 6, ID_FRONT_2CAM + 4,
                ID_FRONT_2CAM + 5, ID_FRONT_2CAM +7};
                nID = id[index];
            }
            break;
            case ID_BACK_1SINK:
            case ID_FRONT_1SINK:
            {
                if(nID == ID_BACK_1SINK)
                {
                    nID = ID_FRONT_1SINK;
                }
                else
                {
                    nID = ID_FRONT_3SELECT +7;
                }
            }
            break;
        }
        if(nID)
	    {
	        SetFocusChild(GetDlgItem(hWnd, nID));
	    }
    }
    else if(key == SCANCODE_ENTER)
    {
        state = !state;
        ushort nID = GetDlgCtrlID(GetFocusChild(hWnd));
       OnBtnCmd(hWnd, nID);
    }
    else if(key == SCANCODE_PERIOD)
    {
        HWND hTwd = GetDlgItem(hWnd, ID_CYCLE_GROUP);
        SetCtrlState(hTwd, !GetCtrlState(hTwd));
        InvalidateRect(hTwd, NULL, TRUE);
        MacCycleButton(hWnd, ID_CYCLE_GROUP);
    }
    else if(key == SCANCODE_0)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
              case TEST_YC:
            {
                if (GetCarrigeData(g_knit_data) !=NULL)
                {
                    carriage_yc_group_exec(crg, group, UP);
                }
            }
            break;
        case TEST_SEL:
            {
                if (GetCarrigeData(g_knit_data) !=NULL)
                {
                    carriage_sel_group_exec(crg, group, UP);
                }
            }
            break;
        case TEST_MOTOR:
            {
                 if (GetCarrigeData(g_knit_data) !=NULL)
                {
                    carriage_motor_group_exec(crg, group, UP);
                }
            }
            break;
        case TEST_CAM:
            {
                if (GetCarrigeData(g_knit_data) !=NULL)
                {
                    carriage_cam_group_exec(crg, group, UP);
                }
            }
            break;
        }

    }
    else if(key == SCANCODE_9)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
              case TEST_YC:
            {
                if (GetCarrigeData(g_knit_data) !=NULL)
                {
                    carriage_yc_group_exec(crg, group, DOWN);
                }
            }
            break;
        case TEST_SEL:
            {
                if (GetCarrigeData(g_knit_data) !=NULL)
                {
                    carriage_sel_group_exec(crg, group, DOWN);
                }
            }
            break;
        case TEST_MOTOR:
            {
                if (GetCarrigeData(g_knit_data) !=NULL)
                {
                    carriage_motor_group_exec(crg, group, DOWN);
                }
            }
            break;
        case TEST_CAM:
            {
                if (GetCarrigeData(g_knit_data) !=NULL)
                {
                    carriage_cam_group_exec(crg, group, DOWN);
                }
            }
            break;
        }

    }
    else if(key == SCANCODE_1)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
              case TEST_YC:
            {
                if (crg !=NULL)
                {
                    carriage_yc_exec(crg, group, 0);
                }
            }
            break;
        case TEST_SEL:
            {
                if (crg !=NULL)
                {
                    carriage_sel_exec(crg, group, 0);
                }
            }
            break;
        case TEST_MOTOR:
            {
                  if (crg !=NULL)
                {
                    carriage_motor_exec(crg, group, 0);
                }
            }
            break;
        case TEST_CAM:
            {
                if (crg !=NULL)
                {
                    carriage_cam_exec(crg, group,0);
                }
            }
            break;
        }
    }
    else if(key == SCANCODE_2)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
              case TEST_YC:
            {
                if (crg !=NULL)
                {
                    carriage_yc_exec(crg, group, 1);
                }
            }
            break;
        case TEST_SEL:
            {
                if (crg !=NULL)
                {
                    carriage_sel_exec(crg, group, 1);
                }
            }
            break;
        case TEST_MOTOR:
            {
                if (crg !=NULL)
                {
                    carriage_motor_exec(crg, group, 1);
                }
            }
            break;
        case TEST_CAM:
            {
                if (crg !=NULL)
                {
                    carriage_cam_exec(crg, group, 1);
                }
            }
            break;
        }
    }
    else if(key == SCANCODE_3)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
              case TEST_YC:
            {
                if (crg !=NULL)
                {
                    carriage_yc_exec(crg, group, 2);
                }
            }
            break;
        case TEST_SEL:
            {
                if (crg !=NULL)
                {
                    carriage_sel_exec(crg, group, 2);
                }
            }
            break;
        case TEST_MOTOR:
            {
                  if (crg !=NULL)
                {
                 //   carriage_motor_exec(crg, group, 2);
                }
            }
            break;
        case TEST_CAM:
            {
                if (crg !=NULL)
                {
                    carriage_cam_exec(crg, group, 2);
                }
            }
            break;
        }
    }
    else if(key == SCANCODE_4)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
              case TEST_YC:
            {
                if (crg !=NULL)
                {
                    carriage_yc_exec(crg, group, 3);
                }
            }
            break;
        case TEST_SEL:
            {
                if (crg !=NULL)
                {
                    carriage_sel_exec(crg, group, 3);
                }
            }
            break;
        case TEST_MOTOR:
            {
                  if (crg !=NULL)
                {
              //      carriage_motor_exec(crg, group, 3);
                }
            }
            break;
        case TEST_CAM:
            {
                if (crg !=NULL)
                {
                    carriage_cam_exec(crg, group, 3);
                }
            }
            break;
        }
    }
    else if(key == SCANCODE_5)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
              case TEST_YC:
            {
                if (crg !=NULL)
                {
                    carriage_yc_exec(crg, group, 4);
                }
            }
            break;
        case TEST_SEL:
            {
                if (crg !=NULL)
                {
                    carriage_sel_exec(crg, group, 4);
                }
            }
            break;
        case TEST_CAM:
            {
                if (crg !=NULL)
                {
                    carriage_cam_exec(crg, group, 5);
                }
            }
            break;
        }
    }
    else if(key == SCANCODE_6)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
            case TEST_YC:
            {
                if (crg !=NULL)
                {
                    carriage_yc_exec(crg, group, 5);
                }
            }
            break;
          case TEST_SEL:
            {
                if (crg !=NULL)
                {
                    carriage_sel_exec(crg, group, 5);
                }
            }
            break;
        case TEST_CAM:
            {
                if (crg !=NULL)
                {
                    carriage_cam_exec(crg, group, 4);
                }
            }
            break;
        }
    }
    else if(key == SCANCODE_7)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
              case TEST_YC:
            {
                if (crg !=NULL)
                {
                    carriage_yc_exec(crg, group, 6);
                }
            }
            break;
        case TEST_SEL:
            {
                if (crg !=NULL)
                {
                    carriage_sel_exec(crg, group, 6);
                }
            }
            break;
        }
    }
    else if(key == SCANCODE_8)
    {
        int group = 0, object = 0;
        GetGroupAndObejct(hWnd, &group, &object);
        CARRIAGE *crg = GetCarrigeData(g_knit_data);

        switch(object)
        {
              case TEST_YC:
            {
                if (crg !=NULL)
                {
                    carriage_yc_exec(crg, group, 7);
                }
            }
            break;
        case TEST_SEL:
            {
                if (crg !=NULL)
                {
                    carriage_sel_exec(crg, group, 7);
                }
            }
            break;
        }
    }
}
///////////////////////////////////////////////////////////////////////////////
/*
 * @param index: 0-整组循环
                 1-单个循环
                 2-全部循环
                 3-复位
 */
 BOOL ctest_can_exec_cycle(HWND hWnd, int index)
{
    int cycle_flag = 0;
    int reset_all = GetCarrigeResetAll(g_knit_data);
    int i = 0;

    if (reset_all)
    {
        return FALSE;
    }

    for (i = 0; i < 3; i++)
    {
        HWND hTmp = GetDlgItem(hWnd, ID_CYCLE_GROUP + i);
        if(GetCtrlState(hTmp))
        {
            cycle_flag = 0x01 << i;
            break;
        }
    }

    if (index == 4)
    {
        if (cycle_flag != 0)
        {
            return FALSE;
        }
    }
    else if (index >= 0 && index <= 2)
    {
        /* 当前循环可以停止 */
        if (cycle_flag != 0 && (cycle_flag & (0x01 << index)) == 0)
        {
            return FALSE;
        }
    }

    return TRUE;
}


static void MacGetFocusnID(HWND hWnd)
{
    int id = GetDlgCtrlID(GetFocusChild(hWnd));
    int idb = GetMacFocusId(g_knit_data);
    HWND wnd = GetDlgItem(hWnd, idb);
    SetCtrlType(wnd, MH_CTRL_TYPE_SEL);
    InvalidateRect(wnd, NULL, TRUE);
    if(id < ID_CYCLE_GROUP)
    {
        SetMacFocusId(g_knit_data, id);
        wnd = GetDlgItem(hWnd, id);
        SetCtrlType(wnd, MH_CTRL_TYPE_CODE);
        InvalidateRect(wnd, NULL, TRUE);
    }
}

void GetGroupAndObejct(HWND hWnd, int *group, int *object)
{
    int focus = GetMacFocusId(g_knit_data);
    switch(focus)
    {
        case (ID_LEFT_1YARN)...(ID_LEFT_18YARN):
            {
                *group = 0;
                *object = TEST_YC;
            }
            break;
        case (ID_RGHT_1YARN)...(ID_RGHT_18YARN):
            {
                *group = 1;
                *object = TEST_YC;
            }
            break;
        case (ID_FRONT_1SELECT)...(ID_FRONT_1SELECT + 7):
            {
                *group = 0;
                *object = TEST_SEL;
            }
            break;
        case (ID_FRONT_2SELECT)...(ID_FRONT_2SELECT + 7):
            {
                *group = 1;
                *object = TEST_SEL;
            }
            break;
        case (ID_FRONT_3SELECT)...(ID_FRONT_3SELECT + 7):
            {
                *group = 4;
                *object = TEST_SEL;
            }
            break;
        case (ID_FRONT_4SELECT)...(ID_FRONT_4SELECT + 7):
             {
                *group = 5;
                *object = TEST_SEL;
            }
            break;
        case (ID_BACK_1SELECT)...(ID_BACK_1SELECT + 7):
             {
                *group = 2;
                *object = TEST_SEL;
            }
            break;
        case (ID_BACK_2SELECT)...(ID_BACK_2SELECT + 7):
             {
                *group = 3;
                *object = TEST_SEL;
            }
            break;
        case (ID_BACK_3SELECT)...(ID_BACK_3SELECT + 7):
             {
                *group = 6;
                *object = TEST_SEL;
            }
            break;
        case (ID_BACK_4SELECT)...(ID_BACK_4SELECT + 7):
             {
                *group = 7;
                *object = TEST_SEL;
            }
            break;
        case (ID_FRONT_1CAM)...(ID_FRONT_1CAM + 8):
             {
                *group = 0;
                *object = TEST_CAM;
            }
            break;
        case (ID_FRONT_2CAM)...(ID_FRONT_2CAM + 8):
            {
                *group = 2;
                *object =TEST_CAM;
            }
            break;
        case (ID_BACK_1CAM)...(ID_BACK_1CAM + 8):
            {
                *group =1;
                *object = TEST_CAM;
            }
            break;
        case (ID_BACK_2CAM)...(ID_BACK_2CAM + 8):
            {
                *group = 3;
                *object = TEST_CAM;
            }
            break;
        case (ID_FRONT_1DUMU)...(ID_FRONT_1DUMU + 3):
            {
                int index = focus - ID_FRONT_1DUMU;
                if(index < 2)
                {
                    *group = 0;
                    *object = TEST_MOTOR;
                }
                else
                {
                    *group = 2;
                    *object = TEST_MOTOR;
                }
            }
            break;
        case (ID_BACK_1DUMU)...(ID_BACK_1DUMU + 3):
            {
                 int index = focus - ID_BACK_1DUMU;
                if(index < 2)
                {
                    *group = 1;
                    *object = TEST_MOTOR;
                }
                else
                {
                    *group =3;
                    *object = TEST_MOTOR;
                }
            }
            break;
        case ID_FRONT_1SINK:
        case ID_BACK_1SINK:
             {
                *group = 4;
                *object = TEST_MOTOR;
            }
           break;
    }
}

 int MacCycleButton(HWND hWnd, int nID)
{
    int index = nID - ID_CYCLE_GROUP;
    HWND hTmp = GetDlgItem(hWnd, nID);
    if (!GetMacFocusId(g_knit_data))
    {
        SetMacFocusId(g_knit_data, ID_BACK_1SELECT);
    }
    int focus = GetMacFocusId(g_knit_data);
    switch (index)
    {
        case 0:         /* 整组循环 */
        case 1:         /* 全部循环 */
        case 2:         /* 单个循环 */
        if (GetCarrigeData(g_knit_data) !=NULL)
        {
            if (GetCtrlState(hTmp))
            {
                int group = 0;
                int object = 0;
                GetGroupAndObejct(hWnd, &group, &object);
                int piece = focus % 10;
                printf("piece = %d\n", piece);
                if (index != 2)     /* 非单个循环 */
                {
                    /* 整组循环，或者全部循环 */
                    piece = 0;
                }
                printf("group = %d, piece = %d, index = %d, to = %d\n", group, piece, index, object);
                carriage_request_cycle(GetCarrigeData(g_knit_data), group, piece, index, object);
            }
            else
            {
                carriage_cancel_cycle(GetCarrigeData(g_knit_data));
            }
        }
        break;
    case 3:         /* 复位 */
        if ((GetCarrigeData(g_knit_data) != NULL))
        {
            carriage_reset_all(GetCarrigeData(g_knit_data));
        }
        break;
    }
    return 0;
}

/* 设置按钮状态 */
static void SetMacButtonState(HWND hWnd)
{
    int i = 0;
    int j = 0;
    if ( GetCarrigeData(g_knit_data) == NULL)
    {
        return;
    }

    /* 选针器 */
    uchar sel_state[8] = { 0 };
    carriage_get_sel_state( GetCarrigeData(g_knit_data), sel_state);
    int sel_id[8] = {ID_FRONT_1SELECT, ID_FRONT_2SELECT, ID_BACK_1SELECT, ID_BACK_2SELECT,
    ID_FRONT_3SELECT, ID_FRONT_4SELECT, ID_BACK_3SELECT, ID_BACK_4SELECT,
    };
    for (i = 0; i < 8; i++)
    {
        for (j = 0; j < 8; j++)
        {
            int down = sel_state[i] & (0x01 << j);
            down = !down;

            HWND hTmp = GetDlgItem(hWnd, sel_id[i] + j);
            if (GetCtrlState(hTmp) != down)
            {
                SetCtrlState(hTmp, down);
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }
    }

       /* 纱嘴 */
    uchar yc_state[2] = { 0 };
    int yarn_id[2] = {ID_LEFT_1YARN, ID_RGHT_1YARN};
    carriage_get_yc_state(GetCarrigeData(g_knit_data), yc_state);

    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 8; j++)
        {
            int down = yc_state[i] & (0x01 << j);
            down = !down;
           HWND hTmp = GetDlgItem(hWnd, yarn_id[i] + j);
            if (GetCtrlState(hTmp) != down)
            {
                SetCtrlState(hTmp, down);
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }
    }

    /* 三角 */
    uchar cam_state[4] = { 0 };
    int cam_id[4] = {ID_FRONT_1CAM, ID_BACK_1CAM, ID_FRONT_2CAM, ID_BACK_2CAM};
    carriage_get_cam_state(GetCarrigeData(g_knit_data), cam_state);
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 6; j++)
        {
            int down = cam_state[i] & (0x01 << j);
            down = !down;
            int id[6] = {5, 6, 7, 0, 1, 2};
           HWND hTmp = GetDlgItem(hWnd, cam_id[i] + id[j]);
            if (GetCtrlState(hTmp) != down)
            {
                SetCtrlState(hTmp, down);
                InvalidateRect(hTmp, NULL, TRUE);
            }
        }
    }

      /* 感应器 */
    char sensor_state[20] = {0};
    HWND wnd;
    carriage_get_sensor_state(GetCarrigeData(g_knit_data), sensor_state, 20);
    for(i = 0; i < 2; i++)
    {
        wnd = GetDlgItem(hWnd, (i + ID_FRONT_DUMU_1SIGN));
        if(GetCtrlState(wnd) != sensor_state[i])
        {
            SetCtrlState(wnd, sensor_state[i]);
            InvalidateRect(wnd, NULL, TRUE);
        }
        wnd = GetDlgItem(hWnd, (i + ID_BACK_DUMU_1SIGN));
        if(GetCtrlState(wnd) != sensor_state[i + 2])
        {
            SetCtrlState(wnd, sensor_state[i + 2]);
            InvalidateRect(wnd, NULL, TRUE);
        }
        wnd = GetDlgItem(hWnd, (i + 2 + ID_FRONT_DUMU_1SIGN));
        if(GetCtrlState(wnd) != sensor_state[i + 4])
        {
            SetCtrlState(wnd, sensor_state[i + 4]);
            InvalidateRect(wnd, NULL, TRUE);
        }
        wnd = GetDlgItem(hWnd, (i + 2 + ID_BACK_DUMU_1SIGN));
        if(GetCtrlState(wnd) != sensor_state[i + 6])
        {
            SetCtrlState(wnd, sensor_state[i + 6]);
            InvalidateRect(wnd, NULL, TRUE);
        }
    }
    //sink sensor
    {
        wnd = GetDlgItem(hWnd, ( ID_BACK_SINK_1SIGN));
        if(GetCtrlState(wnd) != sensor_state[8])
        {
            SetCtrlState(wnd, sensor_state[8]);
            InvalidateRect(wnd, NULL, TRUE);
        }
        wnd = GetDlgItem(hWnd, ( ID_FRONT_SINK_1SIGN));
        if(GetCtrlState(wnd) != sensor_state[9])
        {
            SetCtrlState(wnd, sensor_state[9]);
            InvalidateRect(wnd, NULL, TRUE);
        }
    }

    /* 度目 */
    short value[10] = { 0 };
    carriage_get_motor_pos(GetCarrigeData(g_knit_data), value, 10);
    for (i = 0; i < 2; i++)
    {
          wnd = GetDlgItem(hWnd, (i + ID_FRONT_1DUMU));
        if(GetCtrlState(wnd) != value[i])
        {
            SetCtrlState(wnd, value[i]);
            InvalidateRect(wnd, NULL, TRUE);
        }
        wnd = GetDlgItem(hWnd, (i + ID_BACK_1DUMU));
        if(GetCtrlState(wnd) != value[i + 2])
        {
            SetCtrlState(wnd, value[i + 2]);
            InvalidateRect(wnd, NULL, TRUE);
        }
        wnd = GetDlgItem(hWnd, (i + 2 + ID_FRONT_1DUMU));
        if(GetCtrlState(wnd) != value[i + 4])
        {
            SetCtrlState(wnd, value[i + 4]);
            InvalidateRect(wnd, NULL, TRUE);
        }
        wnd = GetDlgItem(hWnd, (i + 2 + ID_BACK_1DUMU));
        if(GetCtrlState(wnd) != value[i + 6])
        {
            SetCtrlState(wnd, value[i + 6]);
            InvalidateRect(wnd, NULL, TRUE);
        }
    }
     //sink button
    {
        wnd = GetDlgItem(hWnd, ( ID_BACK_1SINK));
        if(GetCtrlState(wnd) != value[8])
        {
            SetCtrlState(wnd, value[8]);
            InvalidateRect(wnd, NULL, TRUE);
        }
        wnd = GetDlgItem(hWnd, ( ID_FRONT_1SINK));
        if(GetCtrlState(wnd) != value[9])
        {
            SetCtrlState(wnd, value[9]);
            InvalidateRect(wnd, NULL, TRUE);
        }
    }


    int warn =  carriage_get_warn(GetCarrigeData(g_knit_data));
    SetMacWarnSign(g_knit_data, warn);
    ShowWarnInfo(hWnd, warn);
//
}

void MacRestore(HWND hWnd)
{
    CARRIAGE *crg = GetCarrigeData(g_knit_data);
    if (!carriage_restore(crg))
    {
        carriage_close(crg);
        PostMessage (hWnd, MSG_CLOSE, 0, 0);
    }
    else
    {
        CreateProgressMacWindow(hWnd);
    }
}

void KeypaneOnKeyDown(HWND hWnd, uint key)
{
    OnKeyDown(hWnd, key);
}

 void *thread_sensor(void *arg)
 {
     HWND hWnd = (HWND)arg;
     while(puase)
     {
        while(state && puase)
        {
            timer--;
            usleep(1000);
        }
        if(!state && timer != DOUBLE_CLICK_TIME)
        {
            if(timer > 0)
            {
                timer = DOUBLE_CLICK_TIME;
                PostMessage(hWnd, MSG_FLUSH, 0, 0);
            }
            else
            {
                timer = DOUBLE_CLICK_TIME;
            }
        }
     }
    pthread_exit(0);
 }

 ///////////////////////////////////////////////////////////////////////////
 /*
 *
 */
static int MacWinProc(HWND hWnd, int message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
    {
    case MSG_CREATE:
        {
        	CreateMacInterface(hWnd);
        	puase = TRUE;
        	state = FALSE;
        	esc_enable = TRUE;
        	timer = DOUBLE_CLICK_TIME;
        	CarriageDateInit(g_knit_data, 50, MOTOR_SPEED, MOTOR_SPEED);
            pthread_create(&tid, NULL, thread_sensor, (void *)hWnd);
        }
        break;
    case MSG_PAINT:
        {
        }
        break;
    case MSG_CLOSE:
        {
            DestroyMainWindow(hWnd);
            MainWindowCleanup(hWnd);
        }
        return 0;
    case MSG_DESTROY:
        {
            ShowWindow(hWnd, SW_HIDE);
            DestroyAllControls(hWnd);
            WND_EXTRA_DATA data;
            data.value = GetWindowAdditionalData(hWnd);
            if(data.pwnd_mac)
            {
                free(data.pwnd_mac);
                SetWindowAdditionalData(hWnd, 0);
            }
        }
        break;
    case MSG_KEYDOWN:
    	{
    	    if(wParam == SCANCODE_ESCAPE)
            {
                if(esc_enable)
                {
                    OnKeyDown(hWnd, wParam);
                    esc_enable = FALSE;
                }
            }
            else
            {
                OnKeyDown(hWnd, wParam);
                MacGetFocusnID(hWnd);
            }
    	}
        return 0;
    case MSG_ERASEBKGND:
        {
            DrawBitmapBkGnd(hWnd, (RECT*)lParam, &g_bmp[BMP_HEAD_TEST]);
        }
    	return 0;
    case MSG_COMMAND:
        {
            state = !state;

        	if(LOWORD(wParam) == ID_ESC)
            {
                if(esc_enable)
                {
                    OnBtnCmd(hWnd, LOWORD(wParam));
                    esc_enable = FALSE;
                }
            }
            else
            {
                OnBtnCmd(hWnd, LOWORD(wParam));
                MacGetFocusnID(hWnd);
            }
        }
    	return 0;
    case MSG_SETDATA:
        {
            int id = GetMacFocusId(g_knit_data);
            switch(id)
            {
                case (ID_FRONT_1DUMU)...(ID_FRONT_1DUMU + 3):
                case (ID_BACK_1DUMU)...(ID_BACK_1DUMU + 3):
                {
                    const char *sc = GetWindowCaption(GetDlgItem(hWnd, ID_EDIT4));
                    int i = 0;
                    for(i = 0; i < 4; i++)
                    {
                        HWND hTmp = GetDlgItem(hWnd, ID_FRONT_1DUMU + i);
                        SetWindowCaption(hTmp, sc);
                        InvalidateRect(hTmp, NULL, TRUE);
                        hTmp = GetDlgItem(hWnd, ID_BACK_1DUMU + i);
                        SetWindowCaption(hTmp, sc);
                        InvalidateRect(hTmp, NULL, TRUE);
                    }
                }
                break;
                case ID_FRONT_1SINK:
                case ID_BACK_1SINK:
                {
                    const char *sc = GetWindowCaption(GetDlgItem(hWnd, ID_EDIT4));
                    int i = 0;
                    for(i = 0; i < 2; i++)
                    {
                        HWND hTmp = GetDlgItem(hWnd, ID_FRONT_1SINK);
                        SetWindowCaption(hTmp, sc);
                        InvalidateRect(hTmp, NULL, TRUE);

                        hTmp = GetDlgItem(hWnd, ID_BACK_1SINK);
                        SetWindowCaption(hTmp, sc);
                        InvalidateRect(hTmp, NULL, TRUE);
                    }
                }
                break;
            }
            CARRIAGE *crg = GetCarrigeData(g_knit_data);
            int sink = atoi(GetWindowCaption(GetDlgItem(hWnd, ID_FRONT_1SINK)));
            int dumu = atoi(GetWindowCaption(GetDlgItem(hWnd, ID_FRONT_1DUMU)));
            carriage_setting_pos(crg, sink, dumu);
        }
        return 0;
    case MSG_IDLE:
        {
             SetMacButtonState(hWnd);
        }
        break;
    case MSG_FLUSH:
        {
             CreateKeypaneMacWindow(hWnd);
        }
        break;
    }
    return DefaultMainWinProc(hWnd, message, wParam, lParam);
}
/*
 *
 */
HWND CreateMacWindow(HWND hHosting)
{
    WND_EXTRA_DATA data;
    SwitchToKnitState(g_knit_data, FALSE);
    data.pwnd_mac = calloc(1, sizeof(WND_MAC));
    MAINWINCREATE CreateInfo;
    CreateInfo.dwStyle = WS_VISIBLE;
    CreateInfo.dwExStyle = WS_EX_NONE;
    CreateInfo.spCaption = "";
    CreateInfo.hMenu = 0;
    CreateInfo.hCursor = GetSystemCursor(0);
    CreateInfo.hIcon = 0;
    CreateInfo.MainWindowProc = MacWinProc;
    CreateInfo.lx = 0;
    CreateInfo.ty = 0;
    CreateInfo.rx = CX_SCREEN;
    CreateInfo.by = CY_SCREEN;
    CreateInfo.iBkColor = COLOR_lightwhite;
    CreateInfo.dwAddData = data.value;
    CreateInfo.hHosting = hHosting;
    HWND hWnd = CreateMainWindow(&CreateInfo);
    if(hWnd != HWND_INVALID)
    {
        CARRIAGE *crg = GetCarrigeData(g_knit_data);
        carriage_set_flush_data(crg, (int)hWnd);
        SetFocusChild(GetDlgItem(hWnd, ID_BACK_1SELECT));
        SetMacFocusId(g_knit_data, ID_BACK_1SELECT);
        HWND wnd = GetDlgItem(hWnd, ID_BACK_1SELECT);
        SetCtrlType(wnd, MH_CTRL_TYPE_CODE);
        InvalidateRect(wnd, NULL, TRUE);
    	ShowWindow(hWnd, SW_SHOWNORMAL);
    	SetMacButtonState(hWnd);
    }
    return hWnd;
}
